﻿using Framework;
using System;
using System.Data;
using System.Runtime.Serialization;
using System.Xml;

namespace SchemaMonitor.xml
{
    [DataContract]
    [KnownType(typeof(CMms))]
    public class CSms
    {
        [DataMember(Order = 1)] public string Address;
        [DataMember(Order = 2)] public string ContactName;
        [DataMember(Order = 3)] public string Subject;
        [DataMember(Order = 4)] public string Body;
        [DataMember(Order = 5)] public string ServiceCenter;
        [DataMember(Order = 6)] public string ReadableDate;

        [DataMember(Order = 7)] public int Type;
        [DataMember(Order = 8)] public int Status;

        [DataMember(Order = 9)] public bool Read;
        [DataMember(Order = 10)] public bool Locked;

        [DataMember(Order = 11)] public long Date;
        [DataMember(Order = 12)] public long DateSent;
        [DataMember(Order = 13)] public DateTime Date_;
        [DataMember(Order = 14)] public DateTime DateSent_;
        [DataMember(Order = 15)] public DateTime DateRead_;


        public CSms(XmlNode i)
        {
            this.Body = CXml.AttributeStr(i, "body");
            this.ContactName = CXml.AttributeStr(i, "contact_name");
            this.Address = CXml.AttributeStr(i, "address");
            this.ServiceCenter = CXml.AttributeStr(i, "service_center");
            this.Subject = CXml.AttributeStr(i, "subject");

            this.Type = CXml.AttributeInt(i, "type");
            this.Status = CXml.AttributeInt(i, "status");

            this.Read = CXml.AttributeBool(i, "read");
            this.Locked = CXml.AttributeBool(i, "locked");

            this.Date = CXml.AttributeLong(i, "date");
            this.DateSent = CXml.AttributeLong(i, "date_sent");
            this.ReadableDate = CXml.AttributeStr(i, "readable_date");

            this.Date_     = (Date     > 0) ? CUtilities.FromUnix(Date)     : DateTime.MinValue;
            this.DateSent_ = (DateSent > 0) ? CUtilities.FromUnix(DateSent) : DateTime.MinValue;
            if (DateTime.MinValue == Date_)
                DateTime.TryParse(ReadableDate, out this.Date_);

            if (Subject == "null") Subject = null;



        }
        public int Save() { return Save(CDataSrc.Default); }
        public int Save(CDataSrc db)
        {
            var nv = GetData();

            var temp = new CNameValueList(nv.Count);
            foreach (var i in nv)
            {
                var key = string.Concat("[", i.Name, "]");
                var obj = i.Value;
                if (null == obj)
                    continue;
                if (obj is DateTime && DateTime.MinValue == (DateTime)obj)
                    continue;
                if (obj is int && int.MinValue == (int)obj)
                    continue;
                temp.Add(key, obj);
            }
            nv = temp;

            return (int)db.Insert("TextMessage", "MessageId", true, nv, null, null);
        }
        protected virtual CNameValueList GetData()
        {
            var nv = new CNameValueList();
            nv.Add("IsMms", false);

            nv.Add("Body", Body);
            nv.Add("ContactName", ContactName);
            nv.Add("Address", Address);
            nv.Add("ServiceCenter", ServiceCenter);
            nv.Add("Subject", Subject);

            nv.Add("Type", Type);
            nv.Add("Status", Status);

            nv.Add("Read", Read);
            nv.Add("Locked", Locked);
			nv.Add("MessageId", Date);


			if (DateTime.MinValue != Date_    ) nv.Add("Date",     Date_);
            if (DateTime.MinValue != DateSent_) nv.Add("DateSent", DateSent_);
            if (DateTime.MinValue != DateRead_) nv.Add("DateRead", DateRead_);


            return nv;
        }
    }
}
